import { defDeclareContainerTemp } from '@/api/downloadTemp'
import goodsRelationModal from '../../components/goodsRelationModal'
import {
    getContainerList
} from '@/api/declare'

/**
 * 集装箱mixins 保存、删除、新增、商品项号等
 */

export default {
    components: {
        goodsRelationModal
    },
    data () {
        return {
            ContainerList: [],
            modalParams: {
                goodsRelationShow: false,
                goodsRelationParams: {}
            }
        }
    },
    methods: {
        initContainerList (data) {
            data.forEach((item, i) => {
                item.containerIndex = i + 1
            })
            this.ContainerList = data.slice()
        },
        // 删除集装箱信息
        delContainerInfo () {
            const id = this.getFormByKey('containerForm', 'containerIndex')
            if (id) {
                this.$Modal.confirm({
                    title: '删除集装箱信息',
                    content: '删除集装箱信息,数据无法恢复,是否确认删除？',
                    onOk: () => {
                        this.ContainerList.splice(id - 1, 1)
                        this.ContainerList.forEach((item, index) => {
                            item.containerIndex = index + 1
                        })
                        this.$Message.success('集装箱信息删除成功！')
                        this.refreshContainer()
                        // 清理审核校验信息
                        const declareContainerId = this.getFormByKey('containerForm', 'declareContainerId')
                        this.clearContainerErrById(declareContainerId)
                    }
                })
            } else {
                this.$Message.warning('请勾选需要删除的集装箱信息！')
            }
        },
        // 刷新集装箱表单
        refreshContainer () {
            this.resetForm('containerForm')
        },
        // 单击集装箱
        getContainerDetails (row) {
            this.setFormData('containerForm', row)
            this.$nextTick(() => {
                // 清空随附单证表单校验信息
                this.clearFormTips('containerForm')
                // 设置审核校验结果
                const id = row.declareContainerId
                if (id && this.containerErrDataList[id]) {
                    const errs = this.containerErrDataList[id]
                    this.setFormTips('containerForm', errs)
                }
            })
        },
        // 保存集装箱
        saveContainerData (data) {
            if (data.contrItems && data.containerNo) { // 如果存在集装箱号，执行保存集装箱信息的代码
                if (!data.containerIndex) { // 新增保存
                    data.containerIndex = this.ContainerList.length + 1
                    this.ContainerList.push(data)
                } else {
                    let index = data.containerIndex - 1
                    this.ContainerList.splice(index, 1, data)
                }
                this.clearContainerErrById(data.declareContainerId, this.getFormTips('containerTips'))
                this.refreshContainer()
            }
        },

        /**
         * 清理集装箱审核校验信息 by 集装箱id
         * @param {String} id 商品id
         * @param {Object} errors 本地校验信息
         */

        clearContainerErrById (id, errors = null) {
            if (id && this.containerErrDataList && this.containerErrDataList[id]) {
                let noErr = true
                if (errors) {
                    const keys = Object.keys(this.containerErrDataList[id])
                    keys.forEach(k => {
                        if (errors[`${k}Msg`]) {
                            noErr = false
                        } else {
                            delete this.containerErrDataList[id][k]
                        }
                    })
                }
                if (noErr) {
                    this.containerErrDataList[id] = null
                }
            }
        },

        // 打开商品项号关系弹窗
        openGoodsRelation () {
            // 判断是否有商品
            if (this.CommInfoDataList.length) {
                const contrItems = this.getFormByKey('containerForm', 'contrItems')
                this.modalParams.goodsRelationParams = Object.freeze({
                    contrItems,
                    CommInfoDataList: this.CommInfoDataList
                })
                this.modalParams.goodsRelationShow = true
            } else {
                this.$Message.warning('请先录入商品信息')
            }
        },
        // 保存商品项号关系
        saveGoodsRelation (v) {
            this.setFormData('containerForm', 'contrItems', v)
            // 校验并保存 todo
            this.validateField('containerForm', 'contrItems', err => {
                if (!err) {
                    this.saveContainerData(this.getFormByKey('containerForm'))
                }
            })
        },
        // 导入集装箱信息
        importContainer () {
            if (!this.form.declareId) {
                this.$Message.warning('请先暂存报关报检信息')
                return false
            }

            this.modalParams.declareInfoImportParams = {
                title: '导入集装箱信息',
                subTitle: '准备单据信息',
                hintTxt: '为提高导入的成功率，请下载并使用系统提供的模版。',
                actionUrl: '/ui/customs/declareContainer/importCustomsDeclareContainer?declareId=' + this.form.declareId,
                downLoadUrl: defDeclareContainerTemp,
                type: this.form.businessType,
                declareId: this.form.declareId,
                importType: 'container'
            }
            this.modalParams.declareInfoImportShow = true
        },
        importContainerSuccess () {
            let params = {
                declareId: this.form.declareId,
                pageNum: 1,
                pageSize: 1000
            }
            getContainerList(params).then(res => {
                if (res.data.code === '0000') {
                    this.initContainerList(res.data.data.list)
                    this.refreshContainer()
                }
            })
        }
    }
}
